﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using KPIS.GERP.GAMS.MODEL;
using KPIS.GERP.GAMS.DAL;
using Telerik.Web.UI;
using System.Collections.Generic;
using System.Globalization;
using KPIS.DBM;


namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_Edit_Inbox : System.Web.UI.Page
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
     
        Commons.Main main = new Commons.Main();
        M_ACM_Depliciate info = new M_ACM_Depliciate();

        protected void Page_Load(object sender, EventArgs e)
        {
            string strQey;
            DataTable dtb = null;
            asset_id.Text = Request.QueryString["asset_id"];
            if (!IsPostBack)
            {
                try
                {
                    dbManager.Open();
                    dbManager.CreateParameters(1);
                    dbManager.AddParameters(0, "@AssetId", asset_id.Text);
                    strQey = @"
select a.AssetId as Assetid,a.AssetName as AssetName,a.Price as Price
, sum(coalesce(b.DepAmount, 0)) as DepAmount, a.LifeYear as LifeYear, max(b.CalculatedDate) as CalculatedDate
, a.AssetDate as AssetDate 
from IcmAsset a 
right join IcmAssetDepreciation b on a.AssetId=b.AssetId 
where a.IsLowerAsset=0
and a.RecordStatus <> 'D'
and a.AssetId=@AssetId
group by a.AssetId,a.AssetName,a.Price,a.LifeYear, a.AssetDate
";
                    dtb = dbManager.ExecuteDataSet(CommandType.Text, strQey).Tables[0];

                    for (int i = 0; i < dtb.Rows.Count; i++)
                    {
                        DataRow dr = dtb.Rows[i];
                        assetname.Text = dr["AssetName"].ToString();
                        DateTime? AssetDate = main.ConvertNullDateTime(dr["AssetDate"]);
                        DateTime? LatestDepreDate = main.ConvertNullDateTime(dr["CalculatedDate"]);
                        double PriceCalInt = main.ConvertNullDouble(dr["Price"]) ?? 0;
                        double LifeYear = main.ConvertNullDouble(dr["LifeYear"]) ?? 0;
                        StratDateCalTax.Text = AssetDate.HasValue ? AssetDate.Value.ToLongDateString() : "-";
                        UpdateCalTax.Text = LatestDepreDate.HasValue ? LatestDepreDate.Value.ToLongDateString() : "-";
                        Price.Text = PriceCalInt.ToString("#,##0.00");
                        AmountDp.Text = (main.ConvertNullDouble(dr["DepAmount"]) ?? 0).ToString("#,##0.00");

                        DPDate.Text = (PriceCalInt / (LifeYear*365)).ToString("#,##0.00");//ค่าเสื่อมต่อวัน
                    }
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }
        }
    }
}